package com.bwie.controller;

import com.bwie.pojo.Result;
import com.bwie.pojo.VGoods;
import com.bwie.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import util.PageUtil;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    //列表
    @GetMapping
    public List<VGoods> list(){
        return goodsService.list();
    }

    @PostMapping("add")
    public Result add(@RequestBody VGoods goods){
        try {
            goodsService.add(goods);
            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }
    @GetMapping("/plist")
    public PageUtil<Map<String,Object>> mohu(@RequestParam(value ="page",defaultValue = "1")int page,
                                             @RequestParam(value = "size",defaultValue = "2")int size,
                                             @RequestParam(value = "goodsname",defaultValue = "")String name){
      Page<Map<String,Object>> mohu= goodsService.mohu(page,size,name);
      return new PageUtil<Map<String,Object>>(page,mohu.getTotalElements(),mohu.getContent(),size);
    }

}
